﻿using KinonekoSoftware.CodariaDB;
using KinonekoSoftware.CodariaDB.Documents.Index;
using KinonekoSoftware.CodariaDB.Documents.TemplateContents.Abilities;
using KinonekoSoftware.CodariaDB.Documents.Theories.Elements;
using KinonekoSoftware.CodariaDB.Documents.Theories.Rarities;
using KinonekoSoftware.CodariaDB.Runtimes;
using Microsoft.Xaml.Behaviors;

namespace KinonekoSoftware.Studio.Services.Renderers
{
    public class AbilityRendererInitializer: Behavior<AbilityRenderer>
    {
        protected override void OnAttached()
        {
            if (AssociatedObject is null)
            {
                return;
            }

            AssociatedObject.DataContextChanged += OnDataContextChanged;
            Setup(AssociatedObject.DataContext as InstanceCache);

            base.OnAttached();
        }

        private void Setup(InstanceCache cache)
        {
            if (cache is null)
            {
                return;
            }

            if (cache.Type != InstanceType.Ability)
            {
                return;
            }

            var runtime        = Ioc.Get<RepositoryRuntime>();
            var repository     = runtime.Repository;
            var propertyMgr    = repository.PropertyManager;
            var engine         = runtime.GetEngine<TemplatedContentEngine>();
            var engine_element = runtime.GetEngine<TheoryEngine_Element>();
            var engine_rarity  = runtime.GetEngine<TheoryEngine_Rarity>();



            var templated = engine.Get<TemplatedAbilityContent>(cache.Id);


            if (templated is null)
            {

                AssociatedObject.Avatar         = cache.Avatar;
                AssociatedObject.AbilityName    = cache.Name;
                AssociatedObject.Intro          = cache.Intro;
                AssociatedObject.Keywords       = null;
                AssociatedObject.Vocabularies   = null;
                AssociatedObject.ElementImageID = null;
                AssociatedObject.RarityBG       = null;
                AssociatedObject.RarityLevel    = 1;
                AssociatedObject.EnableElement  = false;
                AssociatedObject.SetSize(TemplatedContentSize.Normal);

                return;
            }

            if (!string.IsNullOrEmpty(templated.RarityID))
            {
                templated.Rarity = engine_rarity.GetTheory(templated.RarityID);
            }

            if (!string.IsNullOrEmpty(templated.ElementID))
            {
                templated.Element = engine_element.GetTheory(templated.ElementID);
            }

            AssociatedObject.Vocabularies             = new ObservableCollection<ColoredVocabulary>(templated.Vocabularies);
            AssociatedObject.Keywords                 = new ObservableCollection<ColoredKeyword>(templated.Keywords);
            AssociatedObject.Avatar                   = templated.Avatar;
            AssociatedObject.AbilityName              = templated.Name;
            AssociatedObject.Intro                    = templated.Intro;
            AssociatedObject.ElementImageID           = templated.Element?.Image;
            AssociatedObject.RarityLevel              = templated.Rarity?.Level ?? 1;
            AssociatedObject.RarityBG                 = templated.Rarity?.Background;
            AssociatedObject.EnableElement            = true;
            AssociatedObject.EnableLeftTopOverlay     = propertyMgr.EnableAbilityLeftTopMaskLayer;
            AssociatedObject.EnableRightBottomOverlay = propertyMgr.EnableAbilityRightBottomMaskLayer;

            //
            //
            AssociatedObject.SetSize(propertyMgr.AbilityTemplatedContentSize);
        }

        protected override void OnDetaching()
        {

            if (AssociatedObject is null)
            {
                return;
            }

            AssociatedObject.DataContextChanged -= OnDataContextChanged;
            base.OnDetaching();
        }

        private void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (AssociatedObject.DataContext is not InstanceCache cache)
            {
                return;
            }

            Setup(cache);
        }
    }
}